package com.forgenet.game.module.player;


import com.forgenet.message.PacketId;
import com.forgenet.message.rpc.RpcOtherDispatchClientRequest;
import com.forgenet.rpc.IDispatchFacade;
import com.sparrow.common.cron.annotation.Cron;
import com.sparrow.example_message.gm.resp.RespUseGMPacket;
import com.sparrow.redis.cache.mongo.RedisMongoCacheEntity;
import com.sparrow.rpc.util.RpcUtil;
import com.sparrow.socket.anno.Packet;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

import java.lang.annotation.Annotation;

@Document("player")
public class Player implements RedisMongoCacheEntity<Long> {
    @Id
    private Long playerId;

    private String account;

    private String name;

    @Transient
    private String gateId;

    public void setGateId(String gateId) {
        this.gateId = gateId;
    }

    public String getGateId() {
        return gateId;
    }

    public Long getPlayerId() {
        return playerId;
    }

    public void setPlayerId(Long playerId) {
        this.playerId = playerId;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void sendPacket(Object msg) {
        RpcOtherDispatchClientRequest dispatchClientRequest = new RpcOtherDispatchClientRequest();
        Packet an =  msg.getClass().getAnnotation(Packet.class);
        int msgId = an.msgId();
        dispatchClientRequest.setMsgId(msgId);
        dispatchClientRequest.setMsg(msg);
        dispatchClientRequest.setPlayerId(playerId);
        RpcUtil.runAsyncRpcMethod(gateId, IDispatchFacade.class, (service) -> {
            service.dispatchToClientReq(dispatchClientRequest);
        });
    }

    @Override
    public Long getKey() {
        return playerId;
    }

}

